package com.easy.demo.demos.web.service;


import com.easy.demo.demos.web.vo.Role;
import com.easy.demo.demos.web.vo.User;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.easy.activiti.ext.IActivitiCoreService.AuthConfigServer.ActivitiRoleType.ROLE_ACTIVITI_ADMIN;
import static com.easy.activiti.ext.IActivitiCoreService.AuthConfigServer.ActivitiRoleType.ROLE_ACTIVITI_USER;

/**
 * @author zhouchuang
 * @date 2024-11-02 19:54:28
 */
@Service
public class UserService {
    private static Map<String, User> userMapperDb = new HashMap<String, User>(){{
        put("zhouchuang",new User("zhouchuang","周创", Arrays.asList(new Role(ROLE_ACTIVITI_ADMIN.name()),new Role(ROLE_ACTIVITI_USER.name()))));
        put("lisi",new User("lisi","李四", Arrays.asList(new Role(ROLE_ACTIVITI_USER.name()))));
        put("zhangsan",new User("zhangsan","张三", Arrays.asList(new Role(ROLE_ACTIVITI_USER.name()))));
        put("jack",new User("jack","杰克", Arrays.asList(new Role(""))));
    }};

    public void login(String username){
        User user = userMapperDb.get(username);
        Assert.notNull(user,"用户不存在");
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = request.getSession();
        session.setAttribute("user",user);
    }
    public User me(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = request.getSession();
        Object user  =  session.getAttribute("user");
        if(user!=null){
            return (User)user;
        }
        return null;
    }

    public List<User> users(){
        return (List<User>) userMapperDb.values();
    }


    public User byUserName(String username){
        return userMapperDb.get(username);
    }

}
